<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文章添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">

    <script type="text/javascript" th:src="@{/UEditor/ueditor.config.js}" media="all"></script>
    <script type="text/javascript" th:src="@{/UEditor/ueditor.all.js}" media="all"></script>
    <script type="text/javascript" th:src="@{/UEditor/lang/zh-cn/zh-cn.js}" media="all"></script>
    <style>
        .childrenBody{
            padding: 20px;
        }
    </style>
</head>
<body class="childrenBody">
<form class="layui-form" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input class="layui-input" name="name" lay-verify="username" placeholder="请输入名称" type="text">
            <input class="layui-input" name="id" id="id" type="hidden">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">文章内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入文章内容" style="height: 400px;width: 100%;"
                      class="layui-textarea" id="container" name="content">
            </textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input class="layui-input" id="orders" name="orders" lay-verify="required" placeholder="排序号" type="text">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveContainer">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui/layui.js}"></script>

<script type="text/javascript" th:inline="javascript">
    layui.use(['table', 'form', "layer"], function () {
        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , $ = layui.jquery;

        var article = [[${article}]];

        //回显表格数据
        if (article != null) {
            form.val("form", {
                "id": article.id // "name": "value"
                ,"name": article.name
                ,"content": article.content
                ,"orders": article.orders
            });
        }

        // 实例化编辑器
        var ue = UE.getEditor('container');
        // 给富文本复制
        ue.ready(function () {
            var html = ue.getContentTxt();
            if(article.content != ''){
                ue.setContent(article.content);
            }else{
                ue.setContent('');
            }
            ue.execCommand('insertHtml', html);
        })

        // form.verify({
        //     name: function (value, item) { //value：表单的值、item：表单的DOM对象
        //         // if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
        //         //     return '用户名不能有特殊字符';
        //         // }
        //         // if (/(^\_)|(\__)|(\_+$)/.test(value)) {
        //         //     return '用户名首尾不能出现下划线\'_\'';
        //         // }
        //         // if (!new RegExp("^[A-Za-z0-9]+$").test(value)) {
        //         //     return '用户名只能为数字或字母';
        //         // }
        //     }
        // });

        //监听提交
        form.on('submit(saveContainer)', function (data) {
            $.ajax({
                url: [[${basePath}]] + "/group/article/saveArticle", data: data.field, sync: "post", success: function (data) {
                    console.log(data);
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = [[${basePath}]] + "/group/article/index";
                            parent.layer.close(index);
                        }, 1500);
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });

            return false;
        });

    });
</script>
</body>
</html>